<?php
session_start();

include_once ('config.php');
include_once ('saetv2.ex.class.php');
include_once ('../../file_php.php');
require_once ('../../Utils.php');

$postEnable = true;
$wpsite = "http://www.idreems.com/wordpress";
$username = "guest";
$password = "19800219";
$originalTag = iconv("GBK", "UTF-8", "搞笑集锦");
$gbk = false;

define('DEBUG', false);

define('STATUSES', "statuses");
// weibo user_timeline:feature
// $feature 过滤类型ID，0：全部、1：原创、2：图片、3：视频、4：音乐
define('ALL', 0);
define('ORIGINAL', 2);
define('IMAGE', 2);
define('VIDEO', 3);
define('MUSIC', 4);

define('PAGE_COUNT', 2); // 最大页数
define('WEIBO_COUNT', 100); // 每页的weibo数
                            
// Comparison function
function descendByDate ($a, $b)
{
    $dateA = $a["created_at"];
    $dateB = $b["created_at"];
    
    $dateA = strtotime($dateA);
    $dateB = strtotime($dateB);
    
    if ($dateA == $dateB) {
        return 0;
    }
    return ($dateA > $dateB) ? - 1 : 1;
}

$mediationUIDs = array(
        "1953012955" => "追梦者不老"
);

// the following uids should be included in mediationUIDS
$filterUIDs = array(
        "1639356433" => "宅男宅女爱冷笑话",
        "2283030274" => "眼泪都笑出来",
        "2175493427" => "快乐麻花网",
        "1893769523" => "幽默搞笑大王",
        "1713926427" => "微博搞笑排行榜",
        "2684021720" => "恶搞搞笑全球",
        "2615292573" => "幽默搞笑家",
        "2138660531" => "xxoo老湿",
        "1644395354" => "冷笑话精选",
        "2561536543" => "糗事大百科荟萃",
        "3169307223" => "时尚幽默与生活",
        "1948722922" => "每日糗事排行榜",
        "1911808453" => "全球笑话幽默",
        "818201288" => "幽默搞笑家",
        "234583239" => "老根爱讲搞笑的冷笑话",
        "236679" => "冷兔冷笑话",
        "1893752581" => "全球顶尖内涵图",
        "1995444907" => "内涵邪恶漫画精选",
        "1797692797" => "色系军团漫画-邪恶漫画内涵漫画",
        "1921533773" => "23岁的贝壳其实是重口味小清新",
        "2462689840" => "这个漫画好有趣",
        "2391941192" => "漫画集中营",
        "319825888" => "漫画趣味多",
        "210104398" => "18禁漫画微博",
        "2684021720" => "恶搞搞笑全球",
        "336416888" => "幽默搞笑大王",
        "2346323587" => "笑话伤不起",
        "1591142407" => "有趣图片",
        "2906828094" => "逗笑先生",
        "1688574251" => "来福岛冷笑话",
        "1790359634" => "幽默笑话总汇",
        "2041931723" => "好玩图片",
        "1972119462" => "爆笑搞笑幽默冷笑话大全",
        "2561834104" => "IQ_HOLD不住",
        "1727609333" => "哈哈笑话百科",
        "2150182731" => "猪猪爱讲冷笑话",
        "1359660062" => "搞笑能人排行榜",
        "1832538980" => "冷笑话趣图精选",
        "2117146340" => "搞笑图片搞笑GIF图片爆笑动态图",
        "2568798680" => "搞笑幽默哥",
        "2354133130" => "笑得嘴都肿了",
        "2087954890" => "那一刻_我被冷到了",
        "2758798014" => "最搞段子",
        "2412185137" => "那一刻我笑喷了",
        "1751707133" => "瞬间就给笑喷了",
        "2283030274" => "眼泪都笑出来",
        "1739358387" => "搞笑笔记"
);

set_time_limit(0);
$c = new SaeTClientV2(WB_AKEY, WB_SKEY, $_SESSION['token']['access_token']);
$c->set_debug(DEBUG_MODE);
$feeds = array();
foreach ($mediationUIDs as $uid => $name) {
    for ($page = 1; $page <= PAGE_COUNT; ++ $page) {
        $ms = $c->home_timeline($page, WEIBO_COUNT, 0, 0, 0, ORIGINAL);
        if (is_array($ms) && ! empty($ms)) {
            if (DEBUG) {
                echo "status count: " . count($ms[STATUSES]) . "<br/>";
            }
            // filter and merge
            $index = 0;
            foreach ($ms[STATUSES] as $item) {
                // filter uid
                $id = $item["user"]["idstr"];
                if (false == $filterUIDs[$id]) {
                    // echo "idstr: " . $id . " name: " . $item ["user"]
                    // ["name"] . "excluded" . "<br/>";
                    continue;
                }
                array_push($feeds, $item);
                if (DEBUG) {
                    echo $item["created_at"] . "\t" . $item["source"] . "\t" .
                             $item['text'] . "<br/>";
                }
            }
        } else {
            if (DEBUG) {
                echo "name:" . $name . " return null" . "<br/>";
            }
        }
    }
}

// sort results
uasort($feeds, 'descendByDate');
if (DEBUG) {
    echo "sorted " . "size: " . count($feeds) . "<br/>";
}



// new array for json
$jsonArray = array();
foreach ($feeds as $item) {
    $jsonArray[] = $item;
    
    // post to my blog
    if ($postEnable) {
        $originTitle = $item["user"]["description"];
        $originBody = $item["text"];
        
//         if (DEBUG)
        {
            echo "post: ".$originTitle.$originBody."<br/>";
        }
        Utils::postBlog($wpsite, $username, $password, $originTitle, 
                $originBody, $originalTag, $gbk);
        $postEnable = false;
    }
}
$feeds = $jsonArray;

$filename = Utils::getDataPath() . "result.txt";
$zipfilename = Utils::getDataPath() . "result.zip";

// echo
if (is_array($feeds)) {
    if (0 == count($feeds)) {
        return false;
    }
    
    $json = json_encode(array(
            STATUSES => $feeds
    ));
    file_php::writeJson($filename, $json, false);
    
    if (DEBUG) {
        echo $json . "<br/>";
    }
    
    // create zip
    file_php::create_zip(array(
            $filename
    ), $zipfilename, true);
    // file_php::delete($filename);
    if (DEBUG) {
        echo "write to file " . $filename . " count:" . count($feeds) . "<br/>";
    }
    if (DEBUG) {
        foreach ($feeds as $item)
            echo $item["created_at"] . "\t" . $item["source"] . "\t" .
                     $item['text'] . "<br/>";
    }
    return true;
} else {
    echo "error";
    return false;
}
return false;

?>
	
